home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / 3D_Rendering / Musgrave_Shaders / Source / luna.sl < prev    next >
Encoding:
Text File  |  1994-12-08  |  4.6 KB  |  170 lines

  1. /*
  2.  * luna.sl -- surface shader for the moon
  3.  *
  4.  * DESCRIPTION:
  5.  *    Makes a surface that looks sort of like Earth's moon.  It doesn't really
  6.  *    have craters, so it isn't good for closeups.  But it's pretty good at about
  7.  *    the scale for human naked-eye viewing from earth.
  8.  *
  9.  * AUTHOR:
  10.  *    C language version by F. Kenton Musgrave
  11.  *    Translation to Shading Language by Larry Gritz.
  12.  *
  13.  * REFERENCES:
  14.  *    _Texturing and Modeling: A Procedural Approach_, by David S. Ebert, ed.,
  15.  *    F. Kenton Musgrave, Darwyn Peachey, Ken Perlin, and Steven Worley.
  16.  *    Academic Press, 1994.  ISBN 0-12-228760-6.
  17.  *
  18.  * HISTORY:
  19.  *    ??? - original C language version by Ken Musgrave
  20.  *    Apr 94 - translation to Shading Language by L. Gritz
  21.  *
  22.  * this file last updated 18 Apr 1994
  23.  */
  24.  
  25.  
  26. #define snoise(Pt) (2*noise(Pt) - 1)
  27.  
  28.  
  29. #define DNoise(p) (2*(point noise(p)) - point(1,1,1))
  30. #define VLNoise(Pt,scale) (snoise(Pt + scale*DNoise(Pt)))
  31. #define TWOPI (6.28)
  32.  
  33.  
  34. surface
  35. luna (float Ka = .5, Kd = 1;
  36.       float lacunarity = 2;
  37.       float octaves = 8;
  38.       float H = .3;
  39.       color highland_color = .7;
  40.       float maria_basecolor = .7, maria_color = .1;
  41.       float arg22 = 1, arg23 = .3;
  42.       float highland_threshold = -0.2;
  43.       float highland_altitude = 0.001, maria_altitude = 0.0004;
  44.       float peak_rad = .0075, inner_rad = .01, rim_rad = .02, outer_rad = .05;
  45.       float peak_ht = 0.005, rim_ht = 0.003;
  46.       float numrays = 8;  /* arg10 */
  47.       float rayfade = 1;  /* arg11 */
  48.       )
  49. {
  50.   float radial_dist;
  51.   point PP, PQ;
  52.   float l, a, o, i, omega;
  53.   float chaos;
  54.   color Ct;
  55.   float temp1;
  56.   point vv;
  57.   float uu, ht, freq, scale;
  58.   float lighten;
  59.   point NN;
  60.   float pd;  /* pole distance */
  61.   float raydist;
  62.  
  63.   PQ = P;
  64.   PP = transform ("shader", P);
  65.   NN = normalize (N);
  66.   radial_dist = sqrt (xcomp(PP)*xcomp(PP) + ycomp(PP)*ycomp(PP));
  67.   omega = pow (lacunarity, (-.5)-H);
  68.  
  69.   /* bumpy = fBm (PP, omega, lacunarity, octaves); */
  70.   l = 1;  o = 1;  a = 0;
  71.   for (i = 0;  i < octaves;  i += 1) {
  72.       a += o * snoise (l * PP);
  73.       l *= lacunarity;
  74.       o *= omega;
  75.     }
  76.   chaos = a;
  77.  
  78.   Ct = Cs;
  79.  
  80.   /* Insure that the crater is in one of the maria */
  81.   temp1 = radial_dist * arg22;
  82.   if (temp1 < 1)
  83.       chaos -= arg23 * (1 - smoothstep (0, 1, temp1));
  84.  
  85.   if (chaos > highland_threshold) {
  86.       PQ += chaos * highland_altitude * NN;
  87.       Ct += highland_color * chaos;
  88.     }
  89.   else {
  90.       PQ += chaos * maria_altitude * NN;
  91.       Ct *= maria_basecolor + maria_color * chaos;
  92.     }
  93.  
  94.  
  95.   /***********************************************************************/
  96.   /* Add crater */
  97.   /* get normalized vector "v" */
  98.   pd = 1-v;
  99.   vv = point (xcomp(PP)/radial_dist, 0, zcomp(PP)/radial_dist);
  100.   lighten = 0;
  101.   if (pd < peak_rad) {      /* central peak */
  102.       uu = 1 - pd/peak_rad;
  103. /*      lighten = uu*uu; */
  104.       ht = peak_ht * smoothstep (0, 1, uu);
  105.     }
  106.   else if (pd < inner_rad) {       /* crater floor */
  107.       ht = 0;
  108.     }
  109.   else if (pd < rim_rad) {           /* inner rim */
  110.       uu = (pd-inner_rad) / (rim_rad - inner_rad);
  111.       lighten = .75*uu;
  112.       ht = rim_ht * smoothstep (0, 1, uu);
  113.     }
  114.   else if (pd < outer_rad) {        /* outer rim */
  115.       uu = 1 - (pd-rim_rad) / (outer_rad-rim_rad);
  116.       lighten = .75*uu*uu;
  117.       ht = rim_ht * smoothstep (0, 1, uu*uu);
  118.     }
  119.   else ht = 0;
  120.   PQ += ht * NN;
  121.   lighten *= 0.2;
  122.   Ct += color(lighten,lighten,lighten);
  123.  
  124.   /* Add some noise */
  125.   if (uu > 0) {
  126.       if (pd < peak_rad) {     /* if on central peak */
  127.       vv = 5*PP + 3 * vv;
  128.       freq = 1;  scale = 1;  ht = 0;
  129.       for (i = 0;  i < 4;  i += 1) {
  130.           ht += scale * snoise (freq * vv);
  131.           freq *= 2;  scale *= 0.833;
  132.         }
  133. /*      ht = wrinkled (vv, 2, .833, 4); */
  134.       PQ += 0.0025 * uu*ht * NN;
  135.         }
  136.       else {
  137.       vv = 6*PP + 3 * vv;
  138.       freq = 1;  scale = 1;  ht = 0;
  139.       for (i = 0;  i < 4;  i += 1) {
  140.           ht += scale * snoise (freq * vv);
  141.           freq *= 2;  scale *= 0.833;
  142.         }
  143. /*      ht = wrinkled (vv, 2, .833, 4); */
  144.       if (radial_dist > rim_rad)
  145.           uu *= uu;
  146.       PQ += 0.0025 * (0.5*uu + 0.5*ht) * NN;
  147.         }
  148.     }
  149.  
  150.  
  151.   /* Make crater rays (PP, arg10, arg11, arg12, arg15, arg24, arg25, radial_dist);, yielding temp1 */
  152.   lighten = 0;
  153.   if (pd >= rim_rad  &&  pd < 0.4) {
  154.       lighten = smoothstep (.15, .5, snoise(62*u));
  155.       raydist = 0.2 + 0.2 * snoise (20 * mod(u+0.022,1));
  156.       lighten *= (1 - smoothstep (raydist-.2, raydist, pd));
  157.     }
  158.   lighten = 0.2 * clamp (lighten, 0, 1);
  159.   Ct += color (lighten, lighten, lighten);
  160.  
  161.  
  162.   /* Recalc normal since we changed P a whole bunch. */
  163. /*  N = normalize (calculatenormal (PQ)); */
  164.  
  165.   /* Shade like matte */
  166.   Oi = 1;
  167.   Ci = Ct * (Ka * ambient() + Kd * diffuse(faceforward(normalize(N),I)));
  168. }
  169.  
  170.